package com.example.himalayaproject.find.fenlei.adatper;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.fenlei.activity.Fenlei_RecyclerView_item_Activity;
import com.example.himalayaproject.find.fenlei.bean.FenLei_RootBean;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Fenlei_RecyclerViewPagerAdapter extends RecyclerView.Adapter<Fenlei_RecyclerViewPagerAdapter.ViewHolder> implements View.OnClickListener{
    private Context context;
    private List<FenLei_RootBean.Fenlei_Data> list;
    private OnChildClickListener listener;
    private RecyclerView recyclerView;

    public Fenlei_RecyclerViewPagerAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setListener(OnChildClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getId() == 33) {
            return R.layout.fenlei_recyclerview_head;
        } else {
            return R.layout.fenlei_item;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(viewType,parent,false);
        view.setOnClickListener(this);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (list.get(position).getId()==33) {
            Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.imageView);
        }else {
            holder.textView.setText(list.get(position).getTitle());
            Picasso.with(context).load(list.get(position).getCoverPath()).into(holder.itemImageView);
        }


    }


    @Override
    public int getItemCount() {
        return list.size();
    }
    public void addAll(Collection<? extends FenLei_RootBean.Fenlei_Data> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }
    public void start(int position){
        Intent intent=new Intent(context, Fenlei_RecyclerView_item_Activity.class);
        intent.putExtra("text",list.get(position).getTitle());
        context.startActivity(intent);
    }
    @Override
    public void onClick(View view) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            long id = getItemId(position);
            listener.onChildClick(position, id, list.get(position));
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView,itemImageView;
        private TextView textView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.fenlei_head_imageview);
            itemImageView= (ImageView) itemView.findViewById(R.id.fenlei_item_imageview);
            textView= (TextView) itemView.findViewById(R.id.fenlei_item_textview);
        }
    }
    public interface OnChildClickListener {
        void onChildClick(int position, long id, FenLei_RootBean.Fenlei_Data data);
    }
}
